package com.example.springbatch_step_chuck;

import com.example.job_listener.JobAnnStateListener;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.core.listener.JobListenerFactoryBean;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.*;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

/***
 *
 */
@SpringBootApplication
@EnableBatchProcessing //启用springbatch框架 需要的注解
public class SpringBatchApplication7 {

    // job调度器
    @Autowired
    private JobLauncher jobLauncher;

    // job构建工厂
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    // step步骤构建工厂
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    // 自定义step监听器
    @Autowired
    private MyStepListener myStepListener;

    int timer = 10;


    /**
     * 读操作
     * 需要注意的是 只有返回null的时候，读操作才会结束，否则会一直循环进行
     * @return 返回读数据
     */
    @Bean
    public ItemReader<String> itemReader() {

        return new ItemReader<String>() {
            @Override
            public String read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
                int record = timer;
                if (timer -- > 0) {
                    return "read-raw-data : " + record + "\n";

                } else {
                    return null;
                }

            }
        };

    }

    /**
     * 数据处理
     *
     * @return 处理后的数据
     */
    @Bean
    public ItemProcessor<String, String> itemProcessor() {

        return new ItemProcessor<String, String>() {
            @Override
            public String process(String rawData) throws Exception {
                return "data-processed => " + rawData;
            }
        };

    }

    @Autowired
    private MyItemWriter myItemWriter;


    @Bean
    public Tasklet tasklet1() {
        return (stepContribution, chunkContext) -> {


            // 返回执行任务完成
            return RepeatStatus.FINISHED;
        };
    }


    // 作业步骤 不带不读写操作
    @Bean
    public Step step1() {
        // 如果是通过延时加载去获取参数 那么这里的入参就置为null
        return stepBuilderFactory.get("step1")
                .<String, String>chunk(3)   //块状大小  每次的读/操作次数
                .reader(itemReader())// 写操作
                .processor(itemProcessor())  //处理操作
                .writer(myItemWriter)  // 写操作
                .listener(myStepListener)  //加入监听器
//                .tasklet(tasklet1())
                .build();
    }


    @Bean
    public Job job() {
        return jobBuilderFactory
                .get("step-chuck")
                .incrementer(new RunIdIncrementer())    // 使用增量器(run.id自增)
                .start(step1())

                .build();
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBatchApplication7.class, args);
    }

}
